<?php
/* SVN FILE: $Id: item_temporaryban.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Temporary Ban Member item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/other/item_temporaryban.php $
*/

class item_temporaryban extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Temporary Ban a Member";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Use this item to ban a member for a limited time.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Item specific setting
	 *
	 * @access	protected
	 * @var		array
	 */
	protected $itemSettings = array();
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['protected'] = array($this->settings['admin_group']);
			$this->itemSettings['max_day'] = 7;
			$this->itemSettings['subject'] = 'You have been banned!';
			$this->itemSettings['message'] =
<<<EOF
{member_name},

You have been banned from {board_name} starting from {start_time} to {end_time} by {user_name}. You can either wait out the time or have someone use the unban item.
EOF;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		if ( IPSText::getTextClass('editor')->method == 'rte' )
		{
			$message = IPSText::getTextClass('bbcode')->convertForRTE( $this->itemSettings['message'] );
		}
		else
		{
			$message = IPSText::getTextClass('bbcode')->preEditParse( $this->itemSettings['message'] );
		}
		
		$form['protected'] = ipsRegistry::getClass('output')->generateGroupDropdown('item[protected][]', $this->itemSettings['protected'], true);
		$form['max_day'] = ipsRegistry::getClass('output')->formSimpleInput('item[max_day]', $this->itemSettings['max_day']);
		$form['subject'] = ipsRegistry::getClass('output')->formInput('item[subject]', $this->itemSettings['subject']);
		$form['message'] = IPSText::getTextClass('editor')->showEditor( $message, 'item_message' );
		return <<<EOF
				<li>
					<label>{$this->lang->words['groups_protected']}</label>
					{$form['protected']}
				</li>
				<li>
					<label>{$this->lang->words['temp_ban_max_days']}</label>
					{$form['max_day']}
				</li>
				<li>
					<label>{$this->lang->words['temp_ban_subject']}</label>
					{$form['subject']}
				</li>
				<li>
					<label>{$this->lang->words['temp_ban_message']}</label>
					<br />
					{$form['message']}
				</li>
				<li>
					<label>{$this->lang->words['tags']}</label>
					<div style='display: inline-block'>
							{board_name} = {$this->lang->words['temp_ban_board_name']}
							<br />
							{member_name} = {$this->lang->words['temp_ban_member_name']}
							<br />
							{user_name} = {$this->lang->words['temp_ban_user_name']}
							<br />
							{start_time} = {$this->lang->words['temp_ban_start_time']}
							<br />
							{end_time} = {$this->lang->words['temp_ban_end_time']}
							<br />
							{length} = {$this->lang->words['temp_ban_length']}
					</div>
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['protected'] = is_array($input['protected']) ? $input['protected'] : array();
		$setting['max_day'] = intval($input['max_day']);
		$setting['subject'] = $input['subject'];
		$setting['message'] = IPSText::getTextClass('bbcode')->preDbParse(IPSText::getTextClass('editor')->processRawPost('item_message'));
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		if($this->request['request_method'] == 'post')
		{
			$member = IPSMember::load($this->request['member_name'],'all','displayname');
			if(!$member)
			{
				$this->registry->getClass('output')->showError('no_member');
			}
			
			if(in_array($member['member_group_id'],$this->itemSettings['protected']))
			{
				$this->registry->getClass('output')->showError('protected_member');
			}
			
			if($member['temp_ban'])
			{
				$this->registry->getClass('output')->showError('been_banned');
			}
			
			$this->request['time'] = intval($this->request['time']);
			$seconds = $this->request['units'] == 'd' ? 86400 : 3600;
			$duration = $seconds * $this->request['time'];
			if($this->request['time'] <= 0 or $duration > $this->itemSettings['max_day'] * 86400)
			{
				$this->ipsclass->Error(array('MSG'=>'banned_days_wrong'));
			}
			
			$tempBan = IPSMember::processBanEntry(array('timespan' => $this->request['time'],'unit' => $this->request['units']));
			
			IPSMember::save( $member['member_id'], array( 'core' => array( 'temp_ban' => $tempBan ) ) );
			$this->DB->update('members',array(
												'temp_ban' => $tempBan
			),"member_id=".$member['member_id']);
			
			$message = str_replace("{board_name}",$this->settings['board_name'],$this->itemSettings['message']);
			$message = str_replace("{member_name}",$member['members_display_name'],$message);
			$message = str_replace("{user_name}",$this->memberData['members_display_name'],$message);
			$message = str_replace("{start_time}",$this->registry->getClass('class_localization')->getDate(IPS_UNIX_TIME_NOW,'LONG',1),$message);
			$message = str_replace("{end_time}",$this->registry->getClass('class_localization')->getDate(IPS_UNIX_TIME_NOW + $duration,'LONG',1),$message);
			$message = str_replace("{length}",marketFormatter::relativeTimeExact($duration,'LONG'),$message);
			
			IPSText::getTextClass('email')->subject = $this->itemSettings['subject'];
			IPSText::getTextClass('email')->message = $message;
			IPSText::getTextClass('email')->to = $member['email'];
			IPSText::getTextClass('email')->sendMail();
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Banned <a href='{$this->settings['base_url']}showuser={$member['member_id']}'>{$member['members_display_name']}</a> for ".$this->request['time']." ".($this->request['units']));
			
			$this->lang->words['other_member_banned'] = sprintf($this->lang->words['other_member_banned'], $member['members_display_name']);
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['other_member_banned'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		return <<<EOF
<form id='temporaryBanForm' method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='temporaryBan'class='block_wrap'>
		<h3 id='temporaryBanHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<ul>
				<li>
					<label>{$this->lang->words['which_member']}</label>
					<input type='text' name='member_name' value='' size='32' id='member_name' autocomplete='off' class='input_text' />
				</li>
				<li>
					<label>{$this->lang->words['how_long']}</label>
					<input type='text' name='time' value='' size='5' autocomplete='off' class='input_text' style='width:10%' />
					<select name='units' class='input_text' style='width:40%'>
						<option value='h'>{$this->lang->words['hours']}</option>
						<option value='d'>{$this->lang->words['days']}</option>
					</select>
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['temp_ban_this_user']}" name="submit" />
		</fieldset>
	</div>
</form>
<script type='text/javascript'>
if( $('member_name') )
{
	new ipb.Autocomplete( $('member_name'), { multibox: false, url: ipb.vars['base_url'] + 'app=core&module=ajax&section=findnames&do=get-member-names&secure_key={$this->member->form_hash}&name=', templates: { wrap: ipb.templates['autocomplete_wrap'], item: ipb.templates['autocomplete_item'] } } );
}
</script>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
